package com.yihukurama.sysbase.framework.domainservice;

import com.yihukurama.base.app.exception.TipsException;
import com.yihukurama.base.app.utils.TransferUtils;
import com.yihukurama.base.framework.domain.tkmapper.entity.BaseEntity;
import com.yihukurama.base.framework.service.businessservice.impl.SecurityService;
import com.yihukurama.sysbase.common.utils.StringUtils;
import com.yihukurama.sysbase.framework.domain.User;
import com.yihukurama.sysbase.framework.entity.*;
import com.yihukurama.sysbase.framework.mapper.UserpostMapper;
import com.yihukurama.sysbase.framework.mapper.UserroleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihukurama.base.framework.service.domainservice.CrudService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明： User的领域服务
 * @Author: yihukurama
 */
@Service
public class UserService extends CrudService<UserEntity>{

    @Autowired
    UserroleService userroleService;
    @Autowired
    UserpostService userpostService;
    @Autowired
    DeptService deptService;
    @Autowired
    PostService postService;
    @Autowired
    RoleService roleService;
    @Autowired
    SecurityService securityService;


    @Override
    @Transactional
    public UserEntity create(UserEntity userEntity) throws TipsException {

        User user = TransferUtils.transferEntity2Domain(userEntity,User.class);

        //密码加密
        if(!StringUtils.isEmpty(userEntity.getPassword())){
            String password =userEntity.getPassword();
            password = securityService.pwdEncrypt(password);
            userEntity.setPassword(password);
        }
        userEntity = super.create(userEntity);


        List<String> postIds = user.getPostIds();
        List<String> roleIds = user.getRoleIds();


        for (String postId:postIds
             ) {
            UserpostEntity userpostEntity = new UserpostEntity();
            userpostEntity.setPostId(postId);
            userpostEntity.setUserId(userEntity.getId());
            userpostService.create(userpostEntity);
        }


        for (String roleId:roleIds
        ) {
            UserroleEntity userroleEntity = new UserroleEntity();
            userroleEntity.setRoleId(roleId);
            userroleEntity.setUserId(userEntity.getId());
            userroleService.create(userroleEntity);
        }


        return user;
    }

    /**
     * 通过该主体加载出该主体的所有关联信息
     * @param userEntity
     * @return
     * @throws TipsException
     */
    @Override
    public User load(UserEntity userEntity) throws TipsException {
        UserEntity queryUserEntity = super.load(userEntity);

        User queryUser = TransferUtils.transferEntity2Domain(queryUserEntity,User.class);

        //获取部门信息
        DeptEntity deptEntity = new DeptEntity();
        deptEntity.setId(queryUserEntity.getDeptId());
        deptEntity = deptService.load(deptEntity);
        queryUser.setDeptEntity(deptEntity);

        //获取岗位信息
        UserpostEntity userpostEntity = new UserpostEntity();
        userpostEntity.setUserId(userEntity.getId());
        List<UserpostEntity> userpostEntityList =userpostService.list(userpostEntity);
        List<PostEntity> postEntities = new ArrayList<>();
        for (UserpostEntity userpostEntity1:userpostEntityList
        ) {
            PostEntity postEntity = new PostEntity();
            postEntity.setId(userpostEntity1.getPostId());
            postEntity = postService.load(postEntity);
            postEntities.add(postEntity);
        }
        queryUser.setPostEntities(postEntities);
        //获取角色信息
        UserroleEntity userroleEntity = new UserroleEntity();
        userroleEntity.setUserId(userEntity.getId());
        List<UserroleEntity> userroleEntityList =userroleService.list(userroleEntity);
        List<RoleEntity> roleEntities = new ArrayList<>();
        for (UserroleEntity userroleEntity1:userroleEntityList
        ) {
            RoleEntity roleEntity = new RoleEntity();
            roleEntity.setId(userroleEntity1.getRoleId());
            roleEntity = roleService.load(roleEntity);
            roleEntities.add(roleEntity);
        }
        queryUser.setRoleEntities(roleEntities);


        //数据安全，输出用户信息时密码设置为空
        queryUser.setPassword(null);

        return queryUser;
    }


    @Override
    public UserEntity update(UserEntity userEntity) throws TipsException {
        if(!StringUtils.isEmpty(userEntity.getPassword())){
            String password =userEntity.getPassword();
            password = securityService.pwdEncrypt(password);
            userEntity.setPassword(password);
        }
        userEntity = super.update(userEntity);
        userEntity.setPassword(null);
        return userEntity;
    }
}
